<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            width: 520px;
            height: 280px;
            border: 1px solid red;
            margin: 0 auto;
            position: relative;
            /* overflow: hidden; */
        }

        .content {
            position: absolute;
            left: 0px;
            top: 0px;
            width: 2600px;
            height: 280px;
        }

        img {
            float: left;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="content">
            <img src="./imgs/1t.png" alt="">
            <img src="./imgs/2t.png" alt="">
            <img src="./imgs/3t.png" alt="">
            <img src="./imgs/4t.png" alt="">
            <img src="./imgs/5t.png" alt="">
        </div>
    </div>
    <script>
        function move(ele, target, cb) {
            for (let key in target) {
                function run() {
                    // console.log("requestAnimationFrame");
                    let leftNum = parseInt(getComputedStyle(ele, null)[key]);
                    let speed = 5 * (leftNum < target[key] ? 1 : -1);
                    if (leftNum === target[key]) {
                        cb && cb();
                    } else {
                        ele.style[key] = leftNum + speed + "px";
                        // run();
                        requestAnimationFrame(run);
                    }
                }
                run();
            }
        }

        let contentEle = document.querySelector(".content");


        let num = 1; // 当前图片的位置
        // 自动轮播
        // 问题 ：到第五图片的时候 后面图片没有了，也不能回到第一个图片来？
        // 解决：复制第一张图到第五张图后面，
        // 然后第五张图滚到第六张图动画结束后直接把位置调整到第一张图
        setInterval(() => {
            move(contentEle, { left: -num*520 }, function () {
                console.log("运动完成");
            })
            num++;
            if(num==6){
                num = 1;
            }
            console.log(num);
        }, 3000);




    </script>
</body>

</html>